package learner.hmm;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import learner.ObservationPoint;
import learner.Pattern;
import learner.PatternReader;

import org.junit.Test;

public class MovementTest {

	@Test
	public void test() {
		PatternReader reader = new PatternReader();
		Pattern pattern = reader.givePatterns(1).get(0);
		Movement movement = new Movement(pattern);
		List<Map<MovementVector, Double>> freqs = movement.getRelativeFreqMovementIntervals(movement.getSize()/4);
	}

	//persoon moet in juiste eindpunt uitkomen met moves die geen tegels overslaan
	@Test
	public void testInterpolate() {
		final int x1 = 112;
		final int y1 = 4;
		final int x2 = 77;
		final int y2 = 19;
		Pattern pat = new Pattern(new ArrayList<ObservationPoint>(){{
			add(new ObservationPoint(x1,y1,1));
			//KLEIN TIJDSINTERVAL DUS INTERPOLATIE GEWENST
			add(new ObservationPoint(x2,y2,30));
		}});
		Movement movie = new Movement(pat);
		boolean everythingOk = true;
		int totalXDiff= 0;
		int totalYDiff =0;
		for(MovementVector move: movie.getMovementVectors()){
			System.out.println(move.toString());
			totalXDiff+=move.getX();
			totalYDiff+=move.getY();
			if(move.getX()>1 || move.getY()>1 || (move.isStationary()))
				everythingOk=false;
		}
		assertTrue((totalXDiff== x2-x1) && (totalYDiff == y2-y1) && everythingOk);
	}

	//persoon moet in juiste eindpunt uitkomen in 1 move.
	@Test
	public void testDontInterpolate() {
		final int x1 = 112;
		final int y1 = 4;
		final int x2 = 77;
		final int y2 = 19;
		Pattern pat = new Pattern(new ArrayList<ObservationPoint>(){{
			add(new ObservationPoint(x1,y1,1));
			//TIJDSINTERVAL DUS TE GROOT OM INTERPOLATIE TE VERANTWOORDEN
			add(new ObservationPoint(x2,y2,50));
		}});
		Movement movie = new Movement(pat);
		boolean everythingOk = true;
		int totalXDiff= 0;
		int totalYDiff =0;
		for(MovementVector move: movie.getMovementVectors()){
			System.out.println(move.toString());
			totalXDiff+=move.getX();
			totalYDiff+=move.getY();
		}
		if(movie.getMovementVectors().size()!=1)
			everythingOk=false;
		assertTrue((totalXDiff== x2-x1) && (totalYDiff == y2-y1) && everythingOk);
	}

	//gaat naar willekeurige plekken op de brug, kan afhankelijk van de random gegenereerde tijd interpolatie
	//gebruiken of niet (korte tussentijd -> geskipt door te traag bemonsteren -> interpoleren ; grote tussen-
	//tijd -> geskipt doordat echt even geen tegel ingedrukt was -> niet interpoleren), dit test voor een hele
	//hoop random gegenereerde mogelijkheden of de opeenvolging van moves die gegenereerd wordt, de persoon van
	//begin- naar eindpunt brengt.
	@Test
	public void testTotal(){
		Random rand = new Random();
		for(int aantal =0;aantal<50;aantal++){
			final int x1 = rand.nextInt(30)+1;
			final int y1 = rand.nextInt(20)+1;
			final int t1 = rand.nextInt(100)+1;
			final int x2 = rand.nextInt(30)+1;
			final int y2 = rand.nextInt(20)+1;
			final int t2 = rand.nextInt(100)+1;
			final int x3 = rand.nextInt(30)+1;
			final int y3 = rand.nextInt(20)+1;
			final int t3 = rand.nextInt(100)+1;
			final int x4 = rand.nextInt(30)+1;
			final int y4 = rand.nextInt(20)+1;
			final int t4 = rand.nextInt(100)+1;
			Pattern pat = new Pattern(new ArrayList<ObservationPoint>(){{
				add(new ObservationPoint(x1,y1,t1));
				add(new ObservationPoint(x2,y2,t2));
				add(new ObservationPoint(x3, y3,t3));	
				add(new ObservationPoint(x4,y4,t4));
			}});
			Movement movie = new Movement(pat);
			int totalXDiff= 0;
			int totalYDiff =0;
			for(MovementVector move: movie.getMovementVectors()){
				System.out.println(move.toString());
				totalXDiff+=move.getX();
				totalYDiff+=move.getY();
			}
			assertTrue((totalXDiff== x4-x1) && (totalYDiff == y4-y1));
		}
	}
}
